/*
 * @Description: 配置
 * @Author: chenju
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: qingyang
 * @LastEditTime: 2023-06-28 18:44:32
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import { getEmissionList } from '@/apis/system/config/emissionList'
import type { GetEmissionListParams } from '@/apis/system/config/model/emissionListModel'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'

export const useFactorList = () => {
  // 搜索字段及其他
  const searchForm = reactive<GetEmissionListParams>({
    name: null,
    type: null
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '排放源名称',
      field: 'name',
      type: 'input'
    },
    {
      name: '排放源类型',
      field: 'type',
      type: 'select',
      fixedOption: 'emissionTypeOption',
      style: 'width:134px'
    }
  ])

  // 表格配置
  const tableOptions: CPageTableProps['tableOptions'] = {
    selection: false,
    loadData
  }

  function loadData(parameter: BasicPagingListParams) {
    const params: GetEmissionListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getEmissionList(params)
  }

  // 处理参数
  const handleParams = (): GetEmissionListParams => {
    // 参数
    let params: GetEmissionListParams = {
      type: searchForm.type,
      name: searchForm.name
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '排放源名称/编号',
      field: 'name',
      slot: 'nameSlot',
      minWidth: '170',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '排放源类型',
      field: 'type',
      slot: 'typeSlot',
      width: '120',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '排放源描述',
      field: 'description',
      minWidth: '200',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '更新人/更新时间',
      field: 'modifiedBy',
      slot: 'modifiedBySlot',
      width: '200',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '启用状态',
      field: 'status',
      slot: 'statusSlot',
      width: '100',
      fixed: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'op',
      title: '操作',
      field: 'op',
      slot: 'op',
      width: '150',
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
